package com.cqu.lpss.service.impl;

import com.cqu.lpss.mapper.ImageMapper;
import com.cqu.lpss.pojo.Image;
import com.cqu.lpss.pojo.PageBean;
import com.cqu.lpss.service.ImageInterface;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
@Slf4j
@Service
public class ImageImpl implements ImageInterface {
    @Autowired
    ImageMapper imageMapper;
    @Override
    public PageBean<Image> list(int page, int pageSize, String imgUrl, String imgPosition, String imgFormat, String imgName) {
        PageHelper.startPage(page,pageSize);
        List<Image> imageList = imageMapper.list(imgUrl, imgPosition, imgFormat, imgName);
        Page<Image> pagelist = (Page<Image>) imageList;
        PageBean<Image> pageBean = new PageBean<>(pagelist.getTotal(),pagelist);
        return pageBean;
    }

    @Override
    public int add(Image image) {
        image.setImgCreatetime(new Timestamp(System.currentTimeMillis()));
        image.setImgUpdatetime(new Timestamp(System.currentTimeMillis()));
        log.info("创建时间：{}",image.getImgCreatetime());
        int ans=imageMapper.add(image.getImgUrl(),image.getImgName(),image.getImgFormat(),image.getImgPosition(),image.getImgCreatetime(),image.getImgUpdatetime());
        return ans;
    }

    @Override
    public int delete(Integer imgId) {
        int res = imageMapper.deleteById(imgId);
        return res;
    }

    @Override
    public int edit(Image image) {
        image.setImgUpdatetime(new Timestamp(System.currentTimeMillis()));
        int res = imageMapper.update(image.getImgId(),image.getImgUrl(),image.getImgName(),image.getImgFormat(),
                image.getImgPosition(),image.getImgCreatetime(),image.getImgUpdatetime());

        return res;
    }

    @Override
    public List<Image> findByposition(String position) {
        return imageMapper.findByposition(position);
    }
}
